﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QApplication::setQuitOnLastWindowClosed(false);
    this->setWindowIcon(QIcon(":/images/world.png"));

    QGridLayout *gridLayout = new QGridLayout(ui->centralwidget);

    this->m_tabWidget = new QTabWidget(ui->centralwidget);

    this->m_pHttpForm = new HttpForm();
    this->m_tabWidget->insertTab(0, this->m_pHttpForm, "Http");
    connect(this->m_pHttpForm, SIGNAL(statusBarShow(QString)), ui->statusbar, SLOT(showMessage(QString)));

    this->m_pTcpForm = new TcpForm();
    this->m_tabWidget->insertTab(1, this->m_pTcpForm, "Tcp");
    connect(this->m_pTcpForm, SIGNAL(statusBarShow(QString)), ui->statusbar, SLOT(showMessage(QString)));

    this->m_pUdpForm = new UdpForm();
    this->m_tabWidget->insertTab(2, this->m_pUdpForm, "Udp");
    connect(this->m_pUdpForm, SIGNAL(statusBarShow(QString)), ui->statusbar, SLOT(showMessage(QString)));

    gridLayout->addWidget(this->m_tabWidget, 0, 0, 1, 1);

    m_pContextMenu = new QMenu(this);
    m_pSystemTrayIcon = new QSystemTrayIcon(this);
    m_pSystemTrayIcon->setIcon(QIcon(":/images/world.png"));
    m_pSystemTrayIcon->setToolTip(QString::fromLocal8Bit("网络工具"));

    m_pShowNormalAction = new QAction(QIcon(":/images/layout.png"), QString::fromLocal8Bit("显示"),this);
    connect(m_pShowNormalAction, &QAction::triggered, this, &MainWindow::showNormal);
    m_pAboutAction = new QAction(QIcon(":/images/question-balloon.png"), QString::fromLocal8Bit("关于"),this);
    connect(m_pAboutAction, &QAction::triggered, this, &MainWindow::on_about);
    m_pQuitAction = new QAction(QIcon(":/images/control_power_blue.png"), QString::fromLocal8Bit("退出"),this);
    connect(m_pQuitAction, &QAction::triggered, qApp, &QApplication::quit);
    connect(m_pSystemTrayIcon, &QSystemTrayIcon::activated, this, &MainWindow::on_activatedSysTrayIcon);

    m_pContextMenu->addAction(m_pShowNormalAction);
    m_pContextMenu->addAction(m_pAboutAction);
    m_pContextMenu->addSeparator(); //分割
    m_pContextMenu->addAction(m_pQuitAction);
    m_pSystemTrayIcon->setContextMenu(m_pContextMenu);
    m_pSystemTrayIcon->show();
    m_pSystemTrayIcon->showMessage(QString::fromLocal8Bit("Qt网络工具"),//消息窗口标题
            QString::fromLocal8Bit("Http、TCP与Udp工具启动"),//消息内容
            QSystemTrayIcon::MessageIcon::Information,//消息窗口图标
            5000);

    m_pCurrentTime = new QLabel(this);
    m_pCurrentTime->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    ui->statusbar->addPermanentWidget(m_pCurrentTime);
    ui->statusbar->setStyleSheet(QString("QStatusBar::item{border:0px}"));

    m_pTimer = new QTimer(this);
    connect(m_pTimer, &QTimer::timeout, [=](){
        m_pCurrentTime->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    });
    m_pTimer->start(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    qDebug() << "";
   if(m_pSystemTrayIcon->isVisible())
   {
       this->hide();
       event->ignore();
   }
   else
   {
       event->accept();
   }
}

void MainWindow::on_about()
{
    QMessageBox::about(this, QString::fromLocal8Bit("Qt网络工具"), QString::fromLocal8Bit("Http、TCP与Udp工具"));
}

void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
        break;
    case QSystemTrayIcon::DoubleClick:
        this->show();
        break;
    default:
        break;
    }
}
